mastodon_api\methods\builders/
search.rs

1use crate::MastodonClient;
2use crate::error::Result;
3use crate::models::Search;
4
5/// A builder for search requests.
6pub struct SearchBuilder<'a> {
7    client: &'a MastodonClient,
8    query: String,
9    r#type: Option<String>,
10    resolve: Option<bool>,
11    limit: Option<u32>,
12    offset: Option<u32>,
13    account_id: Option<String>,
14    min_id: Option<String>,
15    max_id: Option<String>,
16}
17
18impl<'a> SearchBuilder<'a> {
19    /// Creates a new `SearchBuilder` with the given query.
20    pub fn new(client: &'a MastodonClient, query: &str) -> Self {
21        Self {
22            client,
23            query: query.to_string(),
24            r#type: None,
25            resolve: None,
26            limit: None,
27            offset: None,
28            account_id: None,
29            min_id: None,
30            max_id: None,
31        }
32    }
33
34    /// Sets the type of results to return (accounts, hashtags, statuses).
35    pub fn r#type(mut self, value: &str) -> Self {
36        self.r#type = Some(value.to_string());
37        self
38    }
39
40    /// Whether to resolve accounts and hashtags not known to the local instance.
41    pub fn resolve(mut self, value: bool) -> Self {
42        self.resolve = Some(value);
43        self
44    }
45
46    /// Maximum number of results to return.
47    pub fn limit(mut self, value: u32) -> Self {
48        self.limit = Some(value);
49        self
50    }
51
52    /// Number of results to skip.
53    pub fn offset(mut self, value: u32) -> Self {
54        self.offset = Some(value);
55        self
56    }
57
58    /// Search only for statuses by this account.
59    pub fn account_id(mut self, value: &str) -> Self {
60        self.account_id = Some(value.to_string());
61        self
62    }
63
64    /// Minimum status ID.
65    pub fn min_id(mut self, value: &str) -> Self {
66        self.min_id = Some(value.to_string());
67        self
68    }
69
70    /// Maximum status ID.
71    pub fn max_id(mut self, value: &str) -> Self {
72        self.max_id = Some(value.to_string());
73        self
74    }
75
76    /// Executes the search request (v2).
77    pub async fn send(self) -> Result<Search> {
78        let url = format!("{}/api/v2/search", self.client.base_url());
79        let mut req = self
80            .client
81            .http_client()
82            .get(&url)
83            .query(&[("q", &self.query)]);
84
85        if let Some(t) = self.r#type {
86            req = req.query(&[("type", t)]);
87        }
88        if let Some(r) = self.resolve {
89            req = req.query(&[("resolve", r.to_string())]);
90        }
91        if let Some(l) = self.limit {
92            req = req.query(&[("limit", l.to_string())]);
93        }
94        if let Some(o) = self.offset {
95            req = req.query(&[("offset", o.to_string())]);
96        }
97        if let Some(a) = self.account_id {
98            req = req.query(&[("account_id", a)]);
99        }
100        if let Some(min) = self.min_id {
101            req = req.query(&[("min_id", min)]);
102        }
103        if let Some(max) = self.max_id {
104            req = req.query(&[("max_id", max)]);
105        }
106
107        self.client.send(req).await
108    }
109}